<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Notice extends Controller
{
    public function index()
    {
        return  $this->fetch('notice/notice');
    }

//    显示公告
    public function notice_select()
    {



        $list = Db::name('to_notice')->field(['id', 'desc','status'])->select();
        //return var_dump($list);

//        $this->assign('list',$list);
        return $this->fetch('notice/noticee',['list'=>$list]);


    }

//增加公告
    public function add(){
        return $this->fetch('notice/add');
    }
    public function notice_add(){
        if (request()->isPost()) {
            $data = [

                'id' => input('id'),
                'desc' => input('desc'),
                'status' => input('status')
            ];


//            $validate = \think\Loader::validate('Links');
//            if (!$validate->scene('add')->check($data)) {
//                $this->error($validate->getError());
//                die;
//            };


            if (db('to_notice')->insert($data)) {
                return $this->success('添加公告成功', ('admin/notice/notice_select'));
            } else {
                return $this->error('添加公告失败');
            }
            return;
        }
    }


    public function edit()
    {


        $id = input('id');
        $data = db('to_notice')->find($id);


        $desc = $data['desc'];
        $status=$data['status'];


        return $this->fetch('notice/edit', [
            'id' => $id,
            'desc' => $desc,
            'status' => $status,

        ]);


    }

    public function edits(){
        $id = input('id');

        $data = db('to_notice')->find($id);


        if (request()->isPost()) {
            $data = [
                'id' => $id,
                'desc' => input('desc'),
                'status' => input('status')
            ];


//            $validate = \think\Loader::validate('Links');
//            if (!$validate->check($data)) {
//                $this->error($validate->getError());
//                die;
//            }
            if (db('to_notice')->update($data)) {
                $this->success('修改公告成功',('admin/notice/notice_select'));

            } else {
                $this->error('修改公告失败');
            }
            return;
        }
    }



//    删除公告
    public function del()
    {
        $id =input('id');

        if (db('to_notice')->delete($id)) {

            $this->success('删除公告成功', 'admin/notice/notice_select');
        } else {
            $this->error('删除公告失败');
        }
        return;

        return $this->redirect('admin/notice/notice_select');
    }
    
    
//    隐藏显示
 /*   public function editStatus(){
        $m = Db::name('to_notice')->field(['id', 'desc','status'])->select();
        $id = $_POST['id'];
        $data1['status'] = $_POST['status'];
        $datainfo=$m->where(array('id'=>$id))->update($data1);

        if ($datainfo) {
            $datainfo['status'] = 1;
        } else {
            $datainfo['status'] = 0;
        }
        $this->ajaxReturn($datainfo,'JSON');

    }*/

    public function status(Request $request)
    {
        $data=Request::instance()->post();
        $m = Db::name('to_notice')->where('id',$data['id'])->value('status');

        if ($m==1){
            $list = Db::name('to_notice')->where('id',$data['id'])->update(['status'=>2]);
            return 2;
        }else{
            $list = Db::name('to_notice')->where('id',$data['id'])->update(['status'=>1]);
            return 1;
        }
    }









}
